package com.sz.framework.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;
import com.sz.framework.R;
import com.sz.framework.mvp.IBaseView;
import com.sz.framework.utils.ToastUtil;
import com.sz.framework.widget.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements IBaseView {
    private LoadingDialog loadingDialog;
    protected View mContentView;
    private Unbinder mUnBinder;

    @Override
    public void showToastMessage(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void showToastMessage(int resId) {
        ToastUtil.showToast(resId);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // 避免多次从xml中加载布局文件
        if (mContentView == null) {
            mContentView = inflater.inflate(generateLayout(), null);
            mUnBinder = ButterKnife.bind(this, mContentView);
            initTitleBar();
            initView(savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }
        }
        return mContentView;
    }

    @Nullable
    @Override
    public View getView() {
        return mContentView;
    }

    @Override
    public void showLoadingDialog(String msg) {
        if (null == getActivity() || getActivity().isFinishing()) {
            return;
        }
        getDialog(getActivity(), msg).show();
    }

    @Override
    public void cancelLoadingDialog() {
        if (null != loadingDialog) {
            loadingDialog.cancel();
        }
    }

    @Override
    public void onError(String msg) {
        showToastMessage(msg);
    }

    @Override
    public void goLogin() {
    }

    @NonNull
    private LoadingDialog getDialog(Context context, String content) {
        if (null == loadingDialog) {
            loadingDialog = new LoadingDialog(context);
        }
        if (!TextUtils.isEmpty(content)) {
            loadingDialog.setMessage(content);
        } else {
            loadingDialog.setMessage(getString(R.string.refresh_text));
        }
        return loadingDialog;
    }

    @Override
    public void onDestroy() {
        if (null != loadingDialog) {
            loadingDialog.cancel();
            loadingDialog = null;
        }
        if (mUnBinder != null && mUnBinder != Unbinder.EMPTY) {
            try {
                mUnBinder.unbind();
                mUnBinder = null;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                LogUtils.w("onDestroy: " + e.getMessage());
            }
        }
        super.onDestroy();
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(null);
    }


    /**
     * 初始化View控件
     */
    protected abstract void initView(Bundle savedInstanceState);
}
